package com.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class Config {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=null;

    @Bean("jackson2JsonRedisSerializer")//开启事务的
    public Jackson2JsonRedisSerializer getJacksonSerializer(){
        if (jackson2JsonRedisSerializer==null) {
            jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        return jackson2JsonRedisSerializer;
    }

    @Autowired(required = false)
    LettuceConnectionFactory redisConnectionFactory;

    /**
     * 无事务的
     * @return
     */
    @Bean("redisTemplateMessage")//开启事务的
    public RedisTemplate redisTemplateMessage() {
//        RedisTemplate redisTemplateMessage = createRedisTemplate();
//        if (redisTemplateMessage == null){
//            return null;
//        }

//        redisTemplateMessage.setEnableTransactionSupport(false);//打开事务支持
//        redisTemplateMessage.setHashKeySerializer(RedisSerializer.string()); //key值始终用纯字符串序列化
//        redisTemplateMessage.setHashValueSerializer(getJacksonSerializer());
//        redisTemplateMessage.setKeySerializer(RedisSerializer.string()); //key值始终用纯字符串序列化
//        redisTemplateMessage.setValueSerializer    (getJacksonSerializer());
//        redisTemplateMessage.afterPropertiesSet(); //初始化操作）加载配置后执行


        StringRedisTemplate redisTemplateMessage = new StringRedisTemplate();
//        redisTemplateMessage.setKeySerializer(RedisSerializer.string());
//        redisTemplateMessage.setHashKeySerializer(RedisSerializer.string());
//        redisTemplateMessage.setValueSerializer(RedisSerializer.string());
//        redisTemplateMessage.setHashValueSerializer(RedisSerializer.string());

        redisTemplateMessage.setConnectionFactory(redisConnectionFactory);
        redisTemplateMessage.setEnableTransactionSupport(false);//打开事务支持

        return redisTemplateMessage;
    }



    private RedisTemplate createRedisTemplate() {
        if (redisConnectionFactory == null ) {
            return null;
        }

        RedisTemplate<String, Object> redisTemplateMessage = new RedisTemplate();
        redisTemplateMessage.setConnectionFactory(redisConnectionFactory);

        return redisTemplateMessage;
    }
}
